package org.mindrive.system.service.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.mindrive.system.collection.system.UserEntity;
import org.mindrive.system.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class JwtUserDetailsService implements UserDetailsService {

	@Autowired
	public UserDao userDao;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		QueryWrapper<UserEntity> query = new QueryWrapper<UserEntity>();
		query.eq("user_id", username).eq("status", "1");
		UserEntity user = userDao.getOne(query, false);
		if (user == null) {
			throw new UsernameNotFoundException("用户名不存在: " + username);
		}
		return new User(username, user.getPassword(), new ArrayList<>());
	}

}